﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using OpenSearch;

namespace OpenSearchTests
{
    [TestClass]
    public class ResponseItemTests
    {
        [TestMethod()]
        [DeploymentItem("q_talisman_format_atom.xml")]
        public void ResponseItemTalismanAtom()
        {
            using (System.IO.FileStream responseStream = new System.IO.FileStream("q_talisman_format_atom.xml", System.IO.FileMode.Open))
            {
                Response response = new Response(responseStream);

                Assert.AreEqual(2, response.Items.Count());
                ResponseItem responseItem0 = response.Items.ToList()[0];
                Assert.IsNotNull(responseItem0);
                Assert.AreEqual(0.59509, responseItem0.Relevance, 0.00001);
                Assert.AreEqual("Talisman Saber 2013", responseItem0.Title);
                Assert.AreEqual(new DateTime(2014, 2, 15, 10, 11, 28, DateTimeKind.Utc), responseItem0.LastUpdated);
                Assert.AreEqual(new DateTime(2014, 2, 15, 10, 11, 28, DateTimeKind.Utc), responseItem0.Published);
                Assert.AreEqual("ddf.distribution", responseItem0.FederationResultSourceId);
                Assert.AreEqual("urn:catalog:id:9c5430b4db9c48a58ab6806d1965c025", responseItem0.Id);
                List<Link> links0 = responseItem0.Links.ToList();
                Assert.AreEqual(3, links0.Count);
                Link link00 = links0[0];
                Assert.IsNotNull(link00);
                Assert.AreEqual("http://192.168.40.180:8181/services/catalog/sources/ddf.distribution/9c5430b4db9c48a58ab6806d1965c025?transform=resource", link00.Href.ToString());
                Assert.AreEqual("related", link00.Relation);
                Assert.AreEqual("Get resource", link00.Title);
                Link link01 = links0[1];
                Assert.IsNotNull(link01);
                Assert.AreEqual("http://192.168.40.180:8181/services/catalog/sources/ddf.distribution/9c5430b4db9c48a58ab6806d1965c025", link01.Href.ToString());
                Assert.AreEqual("alternate", link01.Relation);
                Assert.AreEqual("View Complete Metacard", link01.Title);
                Link link02 = links0[2];
                Assert.IsNotNull(link02);
                Assert.AreEqual("http://192.168.40.180:8181/services/catalog/sources/ddf.distribution/9c5430b4db9c48a58ab6806d1965c025?transform=thumbnail", link02.Href.ToString());
                Assert.AreEqual("related", link02.Relation);
                Assert.AreEqual("Get thumbnail", link02.Title);
                ResponseItemContent content0 = responseItem0.Content;
                Assert.IsNotNull(content0);
                Assert.AreEqual("application/xml", content0.MimeType);
                Assert.IsNotNull(content0.Body);
                Assert.AreNotSame("", content0.Body);
                Assert.IsTrue(content0.Body.StartsWith("<content type=\"application/xml\" xmlns=\"http://www.w3.org/2005/Atom\">\r\n\t\t\t<ns3:metacard"));
                Assert.IsTrue(content0.Body.TrimEnd().EndsWith("<ns3:dateTime name=\"modified\">\n\t\t\t\t\t<ns3:value>2014-02-15T21:11:28.966+11:00</ns3:value>\n\t\t\t\t</ns3:dateTime>\n\t\t\t\t<ns3:string name=\"resource-uri\">\n\t\t\t\t\t<ns3:value>content:b1e29c9ed56840a0b5902b4d92005c4a</ns3:value>\n\t\t\t\t</ns3:string>\n\t\t\t</ns3:metacard>\r\n\t\t</content>"));
                ResponseItem responseItem1 = response.Items.ToList()[1];
                Assert.IsNotNull(responseItem1);
                Assert.AreEqual(0.59509, responseItem1.Relevance, 0.00001);
                Assert.AreEqual("130717-N-HI414-185\nCORAL SEA (July 17, 2013) The Arleigh Burke-class guided-missile destroyer USS Momsen (DDG 92) fires its MK 45 5-inch lightweight gun during live gunnery exercise in support of Talisman Saber 2013. Talisman Saber 2013 is a biennial training activity aimed at improving Australian and U.S. combat readiness and interoperability. (U.S. Navy photo by Mass Communication Specialist 3rd Class Gregory A. Harden II/Released)", responseItem1.Title);
                Assert.AreEqual(new DateTime(2014, 2, 15, 10, 11, 29, DateTimeKind.Utc), responseItem1.LastUpdated);
                Assert.AreEqual(new DateTime(2014, 2, 15, 10, 11, 29, DateTimeKind.Utc), responseItem1.Published);
                Assert.AreEqual("ddf.distribution", responseItem1.FederationResultSourceId);
                Assert.AreEqual("urn:catalog:id:d0a3663a87ae47ff9e2347dd6721b37a", responseItem1.Id);
                List<Link> links1 = responseItem1.Links.ToList();
                Assert.AreEqual(3, links1.Count);
                Link link10 = links1[0];
                Assert.IsNotNull(link10);
                Assert.AreEqual("http://192.168.40.180:8181/services/catalog/sources/ddf.distribution/d0a3663a87ae47ff9e2347dd6721b37a?transform=resource", link10.Href.ToString());
                Assert.AreEqual("related", link10.Relation);
                Assert.AreEqual("Get resource", link10.Title);
                Link link11 = links1[1];
                Assert.IsNotNull(link11);
                Assert.AreEqual("http://192.168.40.180:8181/services/catalog/sources/ddf.distribution/d0a3663a87ae47ff9e2347dd6721b37a", link11.Href.ToString());
                Assert.AreEqual("alternate", link11.Relation);
                Assert.AreEqual("View Complete Metacard", link11.Title);
                Link link12 = links1[2];
                Assert.IsNotNull(link12);
                Assert.AreEqual("http://192.168.40.180:8181/services/catalog/sources/ddf.distribution/d0a3663a87ae47ff9e2347dd6721b37a?transform=thumbnail", link12.Href.ToString());
                Assert.AreEqual("related", link12.Relation);
                Assert.AreEqual("Get thumbnail", link12.Title);
            }
        }
    }
}